<template>
  <div class="back-to-top" @click="backToTop" v-show="isShow">
    <span>top</span>
  </div>
</template>

<script>
export default {
  name: "backtop",
  data() {
    return {
      isShow: false,
    };
  },
  created() {
    this.test();
  },
  methods: {
    // 回到页面顶部
    backToTop() {
      window.scrollTo({
        top: 0,
        behavior: "smooth", // 平滑滚动
      });
    },
    test() {
      window.addEventListener("scroll", (a, b) => {
        if (window.scrollY >= 120) {
          this.isShow = true;
        } else {
          this.isShow = false;
        }
      });
    },
  },
};
</script>

<style scoped>
.back-to-top {
  position: fixed;
  right: 20px;
  bottom: 20px;
  z-index: 999;
  background-color: #fff;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  line-height: 30px;
  text-align: center;
  cursor: pointer;
  transition: opacity 0.5s;
  overflow: hidden;
}

.back-to-top:hover {
  opacity: 0.7;
}

.back-to-top span {
  font-size: 12px;
  color: #666;
}
</style>